/** 
 * 文件名：FrameworkInterceptor.java 
 * 版本信息：1.0
 * 日期：2015年10月9日-下午5:44:07   
 */
package meterMonitor.framework.interceptors;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;  

import meterMonitor.framework.context.SessionContext;
import meterMonitor.util.ResourceConfig;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView; 
    

/**
 * FrameworkInterceptor
 *
 * @author zhengfa.li
 * @date 2016年7月18日 上午10:29:23
 * @version
 */
public class FrameworkInterceptor implements HandlerInterceptor{
    
     Logger logger=LoggerFactory.getLogger(FrameworkInterceptor.class);

 	@Autowired
 	protected ResourceConfig resourceConfig;

    /* (non-Javadoc)
     * @see org.springframework.web.servlet.HandlerInterceptor#preHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)
     */
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // TODO Auto-generated method stub
       // logger.info("进入FrameworkInterceptor拦截器preHandle方法,当前线程："+Thread.currentThread()+",时间："+System.currentTimeMillis());
        logger.info("请求url路径为："+request.getRequestURL()); 
        request.setAttribute("ctx", request.getContextPath());
        request.setAttribute("publicModulus", resourceConfig.getValueByKey("publicModulus"));
//        Cookie memberIdCookie=CookieUtil.getCookieByName(request, "MEMBERID");
//    	String memberId=null;
//    	if(memberIdCookie!=null)
//    	{
//    		memberId=java.net.URLDecoder.decode(memberIdCookie.getValue(), "utf-8");
//    	} 
//    	boolean isLogin=true;
//    	//未登录
//		if(memberId==null||memberId.equals("")){
//			isLogin=false;
//		}  
//		else
//		{
//			try{
//				//String decMemberId =
//						DesUtil.decrypt(memberId); 
//			}
//			catch(Exception ex)
//			{
//				logger.error("非法登陆cookie:MEMBERID:"+memberId+";"+ex.getMessage(),ex);
//				isLogin=false;
//			}
//		}
//		//未登录则跳转商城登陆页面
//		if(!isLogin)
//		{
//			String loginUrl=resourceConfig.getValueByKey("member.domain");
//			response.sendRedirect(loginUrl+"Member/Login");
//			return false;
//		} 
        return true ;
    }

    /* (non-Javadoc)
     * @see org.springframework.web.servlet.HandlerInterceptor#postHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.web.servlet.ModelAndView)
     */
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        // TODO Auto-generated method stub
    }

    /* (non-Javadoc)
     * @see org.springframework.web.servlet.HandlerInterceptor#afterCompletion(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception)
     */
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        // TODO Auto-generated method stub
       // logger.info("进入FrameworkInterceptor拦截器afterCompletion方法,当前线程："+Thread.currentThread()+",时间："+System.currentTimeMillis());
        // 清除ThreadLocal中持有的信息
        SessionContext.remove();
    }

}
